# build gazebo from source
###
 # @Author: Lai Jun
 # @E-mail: laijun@nudt.edu.cn
 # @LastEditors: Lai Jun
 # @LastEditTime: 2021-12-07 09:26:32
### 
# 他会卸载以前所留下的gazebo, 然后重新装一个gazebo_9.16, 并且是编译的是一个可debug的版本
# 注意可能需要包括gazebo所在的路径才能真正运行
# 可能会遇到ignition-math等包未找到的问题，请自行搜索解决（可以通过apt装，也可以通过源码编译）
# 在安装之前可以自行调整系统的apt源地址
# TODO tested only with gazebo 9.16.0 in ubuntu 18.04
set -x 
GAZEBO_VERSION_TAG=gazebo9_9.16.0
UBUNTU_DIST=$(lsb_release -sc)
sudo apt-get remove -y '.*gazebo.*' '.*sdformat.*' '.*ignition-math.*' '.*ignition-msgs.*' '.*ignition-transport.*'
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ $UBUNTU_DIST main" > /etc/apt/sources.list.d/ros1-latest.list && apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

sudo apt-get update && sudo apt-get install -y apt-utils build-essential psmisc vim wget

wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add - \
&& sudo apt-get update \
&& sudo apt-get install -y libsdformat6-dev libignition-math4-dev  libignition-transport4-dev libignition-msgs-dev

wget https://gitee.com/mfary/release-tools/raw/master/jenkins-scripts/lib/dependencies_archive.sh -O /tmp/dependencies.sh \
&& chmod +x /tmp/dependencies.sh \
&& GAZEBO_MAJOR_VERSION=${BUILD_GAZEBO_MAJOR_VERSION} ROS_DISTRO=${BUILD_ROS_DISTRO} /tmp/dependencies.sh && echo $BASE_DEPENDENCIES $GAZEBO_BASE_DEPENDENCIES | tr -d '\\' | xargs sudo apt-get -y install

cd  git clone https://gitee.com/hill227/gazebo /tmp/gazebo \
    && cd /tmp/gazebo \
    && git checkout $GAZEBO_VERSION_TAG \
    && mkdir build \
    && cd build \
    && cmake -DCMAKE_BUILD_TYPE=Debug ../ \
    && cmake ../  \
    && make -j4 \
    && sudo make install 
